package com.atguigu.serviceedu.controller;


import com.atguigu.commonutils.R;
import com.atguigu.serviceedu.entity.EduChapter;
import com.atguigu.serviceedu.entity.chapter.ChapterVo;
import com.atguigu.serviceedu.service.EduChapterService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2022-04-06
 */
@RestController
@RequestMapping("/serviceedu/edu-chapter")
@CrossOrigin
public class EduChapterController {
    @Autowired
    private EduChapterService chapterService;

    /**
     * 课程大纲列表，根据课程id进行查询
     *
     * @return
     */
    @ApiOperation(value = "嵌套章节数据列表")
    @GetMapping("getChapterVideo/{courseId}")
    public R getChapterVideo(@PathVariable String courseId) {
        List<ChapterVo> list = chapterService.getChapterVideoByCourseId(courseId);


        return R.ok().data("allChapterVideo", list);

    }

    /**添加章节
     * @param eduChapter
     * @return
     */
    @PostMapping("addChapter")
    public R addChapter(@RequestBody EduChapter eduChapter) {
       chapterService.save(eduChapter);
        return R.ok();

    }

    /**
     * 根据章节id查询
     * @param chapterId
     * @return
     */
   @GetMapping("getChapterInfo/{chapterId}")
    public R getChapterInfo(@PathVariable String chapterId){
       EduChapter eduChapter = chapterService.getById(chapterId);
        return R.ok().data("chapter",eduChapter);
   }

    /**
     * 修改章节
     * @param eduChapter
     * @return
     */
   @PostMapping("updateChapter")
       public R updateChapter(@RequestBody EduChapter eduChapter){
       chapterService.updateById(eduChapter);
       return R.ok();
       }

    /**
     * 删除章节
     * @param chapterId
     * @return
     */
    @ApiOperation(value = "根据ID删除章节")
    @DeleteMapping("{chapterId}")
    public R deleteChapter(
            @ApiParam(name = "chapterId", value = "章节ID", required = true)
            @PathVariable String chapterId){

        boolean result = chapterService.deleteChapter(chapterId);
        if(result){
            return R.ok();
        }else{
            return R.error().message("删除失败");
        }
    }
}

